<template>
  <div>
    <!-- 导航栏 -->
    <van-nav-bar
      title="设置昵称"
      right-text="完成"
      left-text="取消"
      @click-left="$emit('close')"
      @click-right="onClickRight"
    />
    <!-- /导航栏 -->
    <div style="padding: 10px;">
      <van-field
        v-model.trim="localNmae"
        rows="2"
        autosize
        type="textarea"
        maxlength="7"
        placeholder="请输入新昵称"
        show-word-limit
      />
    </div>
  </div>
</template>

<script>
import { updateUserName } from '@/api/user.js'
export default {
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      localNmae: this.value
    }
  },
  methods: {
    async onClickRight () {
      this.$toast.loading({
        message: '保存中...',
        forbidClick: true,
        duration: 0
      })
      try {
        if (this.localNmae.trim() === '') return this.$toast('不能为空')
        await updateUserName({
          name: this.localNmae
        })
        this.$emit('close')
        this.$emit('input', this.localNmae)
        this.$toast.success('更新昵称成功')
      } catch (err) {
        this.$toast('更新昵称失败')
      }
    }
  }
}
</script>

<style></style>
